import { ImgFormat, ImgName } from './ImgFormat.js'
import { DayTime, DayTimeSecond } from './DayTime.js'


export const UploadImg = (item) => {
	console.log(item);
	let path = ''
	path = `PostImg/${DayTime}/${ImgName(item.name)}_${DayTimeSecond}.${ImgFormat(item.name)}`
	/*  
		微信小程序端BUG 
		- up-upload组件无法获取文件名字 
		- 采用时间戳+随机字符作为文件名
	*/
	// #ifdef MP-WEIXIN
		console.log('WX');
		path = `PostImg/${DayTime}/${Date.now()}_${Math.random().toString(36).substring(2, 8)}.${ImgFormat(item.url)}`
	// #endif
	
	return new Promise(async (resolve) => {
		await uniCloud.uploadFile({
			filePath: item.url,
			cloudPath: path,
			success: (res) => {
				console.log(res) // fileID
				resolve({url: res.fileID})
			}
		})
	})
}